package ro.myApp.security.client;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;

import ro.myApp.client.BaseView;
import ro.myApp.security.dto.UserChangePasswordMessage;
import ro.myApp.security.dto.UserCheckReminderCodeMessage;
import ro.myApp.security.entities.User;

@Name("changePassword")
@BypassInterceptors
@Scope(ScopeType.PAGE)
public class ChangePassword extends BaseView {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2022641496131002697L;

	private String code;
	
	private String username;
	
	private String password;
	
	private User instance;
	
	@Logger
	private Log log;
	
	public void checkUser() {
		Object result = sendMessage(new UserCheckReminderCodeMessage(username, code));
		if (result instanceof User){
			instance=(User) result;
		}
	}
	
	public void changePassword () {
		log.debug("Change password for user with parameters: code = {0} and username = {1}", code, username);
		instance.setPassword(password);
		Object result = sendMessage (new UserChangePasswordMessage(instance));
		if (result instanceof User){
			Identity.instance().getCredentials().setUsername(username);
			Identity.instance().getCredentials().setPassword(instance.getPassword());
			Identity.instance().login();
		}
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public User getInstance() {
		return instance;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
